var leftbar = {
    name: "Left Bar",
    speed: 30, // velocità in px/frame
    x: 0,
    y: 0,
    width: 10,
    height: 20,
     
    draw: function(ctx) {
            // disegnamo la barra sinistra
            ctx.fillStyle = "#fff";
            ctx.fillRect(this.x, this.y, this.width , this.height);
          }
};

var rightbar = {
	name: "Right Bar",
    speed: 30, // velocità in px/frame
    x: 0,
    y: 0,
    width: 10,
    height: 20,
     
    draw: function(ctx) {
            // disegnamo la barra sinistra
            ctx.fillStyle = "#fff";
            ctx.fillRect(this.x, this.y, this.width , this.height);
          }
};
 
var ball = {
     
    getLeftExtreme: function(){
    	return this.x-this.radius;
    }, 
    
    getRightExtreme: function(){
    	return this.x+this.radius;
    }, 
     
    setup: function(W,H){
    	//console.log('SETUP PALLA!');
    	// direzione, inizialmente casuale
    	this.angle=Math.random() * Math.PI * 2;
    	this.x =  Math.ceil(W/2);
    	this.y =  Math.ceil(H/2);
    	this.radius = Math.ceil(W/100*1);
    	this.speed = 3; // velocità in px/frame
    },
     
    draw: function(ctx) {
            // disegnamo la barra sinistra
            ctx.fillStyle = "#fff";
            ctx.beginPath();  
            ctx.arc(this.x, this.y, this.radius, 0, Math.PI*2, false);  
            ctx.closePath();  
            ctx.fill(); 
          },
           
    move: function(ctx) {
            this.x += Math.cos(this.angle) * this.speed;
            this.y += Math.sin(this.angle) * this.speed;
         },
          
	collide: function(bar) {
		var barTop=bar.y;
		var barBottom=bar.y+bar.height;
		//console.log(bar.name+':'+barTop+'-'+barBottom);
		if((bar.name=='Left Bar' && ((this.getLeftExtreme())<(bar.x+bar.width))) || (bar.name=='Right Bar' && ((this.getRightExtreme())>(bar.x))) ) //la barra destra non ha bisogno dello spessore
			if(this.y>barTop && this.y<barBottom){
				console.log('bar.x + bar.width:' + (bar.x+bar.width));
				console.log('ball.x-ball.radius' + (this.x-this.radius));
				console.log('ball x:' +this.x);
				console.log('ball radius:' + this.radius);
				return true;
			}
		return false;	
	},
	
	scored: function(bar) {
		if((bar.name=='Left Bar' && ((this.x-this.radius)<bar.x)) || (bar.name=='Right Bar' && ((this.x + ball.radius)>bar.x)) ){ 
			console.log('scored on '+bar.name+'!');
			return true;	
		}
		return false;
	}
	
	
};